package wkt.notice;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import net.xnzn.framework.secure.filter.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/notice-tips")
@Validated
public class NoticeTipsController {
    @Autowired
    private NoticeTipsMapper noticeTipsMapper;

    @PostMapping("/get")
    public NoticeTips get() {
        return noticeTipsMapper.selectOne(Wrappers.lambdaQuery(NoticeTips.class)
                .select(NoticeTips::getFirstLine, NoticeTips::getFirstLineColor, NoticeTips::getSecondLine, NoticeTips::getSecondLineColor));
    }

    @PostMapping("/upsert")
    @Transactional(rollbackFor = Exception.class)
    @RequiresAuthentication
    public void update(@RequestBody NoticeTips noticeTips) {
        final NoticeTips noticeTips1 = get();
        if (noticeTips1 == null) {
            noticeTipsMapper.insert(noticeTips);
            return;
        }

        noticeTipsMapper.update(null, Wrappers.lambdaUpdate(NoticeTips.class)
                .set(NoticeTips::getFirstLine, noticeTips.getFirstLine())
                .set(NoticeTips::getFirstLineColor, noticeTips.getFirstLineColor())
                .set(NoticeTips::getSecondLine, noticeTips.getSecondLine())
                .set(NoticeTips::getSecondLineColor, noticeTips.getSecondLineColor())
        );
    }
}
